package com.imooc.mpimplementationpermissions.controller;

import com.imooc.mpimplementationpermissions.dto.LoginDTO;
import com.imooc.mpimplementationpermissions.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

/**
 * @author: Dk.L
 * @date: 2023/4/3
 * @Description: 登录控制器
 */

@Controller
@RequestMapping("/auth")
public class LoginController {

    @Autowired
    private AccountService accountService;

    /**
     * 用户登录
     *
     * @param username
     * @param password
     * @return
     */
    @PostMapping("/login")
    public String login(String username, String password, HttpSession session, RedirectAttributes attributes) {
        LoginDTO loginDTO = accountService.login(username, password);
        String error = loginDTO.getError();
        if (null == error) {
            session.setAttribute("account", loginDTO.getAccount());
        } else {
            // 前端就能取到error值显示出来
            attributes.addFlashAttribute("error", error);
        }
        return loginDTO.getPath();

    }

}
